package cn.bigcypress.controller.backend;

import cn.bigcypress.common.base.BaseController;
import cn.bigcypress.common.base.ServerResponse;
import cn.bigcypress.pojo.dto.BlogAddRequest;
import cn.bigcypress.pojo.dto.BlogListRequest;
import cn.bigcypress.pojo.vo.BlogDetail4AdminVO;
import cn.bigcypress.pojo.vo.BlogDetail4UserVO;
import cn.bigcypress.service.BlogService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*博客管理端Controller*/
@RequestMapping("manage/blogs")
@Api(tags = "博客管理模块")
@RestController
public class ManageBlogController extends BaseController {

    @Autowired
    private BlogService blogService;

    @ApiOperation(value ="新增话题")
    @PostMapping
    public ServerResponse<Boolean> addBlog(@RequestBody BlogAddRequest blogAddRequest){
        validate(blogAddRequest);

        return blogService.createBlogInfo(blogAddRequest);
    }

    @GetMapping
    @ApiOperation(value ="分页查询列表博客",response = BlogDetail4UserVO.class,responseContainer = "List")
    public ServerResponse<PageInfo<BlogDetail4UserVO>> getBlogList(@ModelAttribute BlogListRequest blogListRequest){
        return blogService.getBlogListInfos(blogListRequest);
    }

    @PutMapping("/{id:\\d+}")
    @ApiOperation(value ="修改话题")
    public ServerResponse<Boolean> updateBlog(@ApiParam("话题id") @PathVariable Integer id,
                                              @RequestBody BlogAddRequest blogAddRequest){
        validate(blogAddRequest);
        return blogService.updateBlog(id, blogAddRequest);
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation(value ="查找话题",response= BlogDetail4UserVO.class)
    public ServerResponse<BlogDetail4AdminVO> getBlog(@ApiParam("话题id") @PathVariable Integer id){

        return blogService.getBlogDetailInfo(id);
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation(value ="删除话题")
    public ServerResponse<Boolean> deleteBlog(@ApiParam("话题id") @PathVariable Integer id){

        return blogService.deleteBlogInfo(id);
    }

}
